// https://www.acwing.com/problem/content/156/

#include <iostream>
#include <vector>
#include <deque>
using namespace std;


/*
8 1
1 3 -1 -3 5 3 6 7
 */
int main() {
    int n, k;
    cin>>n>>k;
    vector<int> nums(n);
    for (int i=0; i<n; i++) {
        cin>>nums[i];
    }

    // min
    deque<int> min_deq;
    for (int i=0; i<n; i++) {
        // 新进入窗口的值小于队尾元素，则队尾出队列
        while (!min_deq.empty() && min_deq.back() > nums[i]) {
            min_deq.pop_back();
        }
        min_deq.push_back(nums[i]);
        if (i >= k && min_deq.front() == nums[i - k]) {
            min_deq.pop_front();
        }
        if (i + 1 >= k) {
            cout<<min_deq.front()<<" ";
        }
    }
    cout<<endl;

    // max
    deque<int> max_deq;
    for (int i=0; i<n; i++) {
        while (!max_deq.empty() && max_deq.back() < nums[i]) {
            max_deq.pop_back();
        }
        max_deq.push_back(nums[i]);
        if (i >= k && max_deq.front() == nums[i-k]) {
            max_deq.pop_front();
        }
        if (i + 1 >= k) {
            cout<<max_deq.front()<<" ";
        }
    }
    cout<<endl;
    return 0;
}
